package com.newcoin.xgb.controller;

import com.newcoin.xgb.dto.CancelOrSubmitDto;
import com.newcoin.xgb.dto.GrabAnOrderDto;
import com.newcoin.xgb.dto.QueryParameterDto;
import com.newcoin.xgb.entity.Result;
import com.newcoin.xgb.entity.XgbGrabOrdersRecord;
import com.newcoin.xgb.po.GrabOrderGoodsPo;
import com.newcoin.xgb.po.GrabPageInfoPo;
import com.newcoin.xgb.service.GrabOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
@RequestMapping(value = "/xgb")
@Api(description = "乐刷抢购接口")
public class GrabOrderController {

    @Autowired
    private GrabOrderService service;

    @ApiOperation(value = "自动抢单")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "userId", value = "用户id", required = true, dataType = "String"),
                    @ApiImplicitParam(name = "token", value = "token", required = true, dataType = "String"),
                    @ApiImplicitParam(name = "areaId", value = "抢单区id", required = true, dataType = "String")
            }
    )
    @PostMapping(value = "/autoOrder")
    public Result<XgbGrabOrdersRecord> autoOrder(GrabAnOrderDto grabAnOrderDto) {
        try {
            return service.autoOrder(grabAnOrderDto);
        } catch (Exception e) {
            log.error(e.getMessage());
            Result result = new Result();
            result.setCode(500);
            result.setMessage("自动抢单失败");
            return result;
        }
    }


    @ApiOperation(value = "取消或提交订单")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "userId", value = "用户id", required = true, dataType = "String"),
                    @ApiImplicitParam(name = "token", value = "token", required = true, dataType = "String"),
                    @ApiImplicitParam(name = "orderId", value = "订单id", required = true, dataType = "String"),
                    @ApiImplicitParam(name = "type", value = "类型  0 取消订单 1 提交订单", required = true, dataType = "int"),
            }
    )
    @PostMapping(value = "/cancelOrSubmit")
    public Result cancelOrSubmit(CancelOrSubmitDto cancelOrSubmitDto) {
        try {
            return service.cancelOrSubmit(cancelOrSubmitDto);
        } catch (Exception e) {
            log.error(e.getMessage());
            Result result = new Result();
            result.setCode(500);
            result.setMessage("失败");
            return result;
        }
    }


    @ApiOperation(value = "获取抢购区今日战果数据")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "userId", value = "用户id", required = true, dataType = "String"),
                    @ApiImplicitParam(name = "token", value = "token", required = true, dataType = "String"),
                    @ApiImplicitParam(name = "", value = "忽略非必填参数", required = false)
            }
    )
    @GetMapping(value = "/getTodayVictory")
    public Result<GrabPageInfoPo> getTodayVictory(QueryParameterDto queryParameterDto) {
        try {
            return service.getTodayVictory(queryParameterDto);
        } catch (Exception e) {
            log.error(e.getMessage());
            Result<GrabPageInfoPo> result = new Result();
            result.setCode(500);
            result.setMessage("获取今日战果数据失败");
            return result;
        }
    }
}
